package com.amigo.online.provider.compare.quartzjob.full;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.FutureTask;
import java.util.stream.Collectors;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.amigo.online.provider.common.compare.AbstractBasicCompareService;
import com.amigo.online.provider.common.entity.UserMongoInfo;
import com.amigo.online.provider.common.entity.UserMysqlInfo;
import com.amigo.online.provider.common.entity.UserSolrInfo;
import com.amigo.online.provider.compare.quartzjob.AbstractUserComparisonJob;
import com.amigo.online.provider.compare.service.UserComparisonService;
import com.amigo.online.provider.compare.task.MongoUserTask;
import com.amigo.online.provider.compare.task.MysqlUserTask;
import com.amigo.online.provider.compare.task.SolrUserTask;
import com.amigo.online.provider.sync.handle.data.mongo.service.UserMongoService;
import com.amigo.online.provider.sync.handle.data.solr.service.UserSolrService;
import com.amigo.online.provider.sync.handle.service.DataHandler;

/**
 * @author amigo
 */

public class UserInfoComparisonJob extends AbstractUserComparisonJob implements Job {

    @Autowired
    ExecutorService executorService;

    @Autowired
    UserComparisonService userComparisonService;

    @Autowired
    UserMongoService userMongoService;

    @Autowired
    UserSolrService userSolrService;

    @Autowired
    @Qualifier("userMongoDataHandler")
    DataHandler userMongoDataHandler;

    @Autowired
    @Qualifier("userSolrDataHandler")
    DataHandler userSolrDataHandler;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {

        List<UserMongoInfo> userMongoList = new ArrayList<>();
        List<UserSolrInfo> userSolrList = new ArrayList<>();
        List<UserMysqlInfo> userMysqlList = new ArrayList<>();
        context.getJobDetail();
        // mysql 和mongo 数据比对
        System.out.println("Mysql-------to----------mongo");

        int maxId = userComparisonService.getMaxId();
        // 异步操作，把查询的数据塞入到全局变量中
        int index = maxId / 1000 + 1;

        List<FutureTask<List<UserMongoInfo>>> mongofutureList = new ArrayList<>(maxId);
        List<FutureTask<List<UserMysqlInfo>>> mysqlfutureList = new ArrayList<>(maxId);
        List<FutureTask<List<UserSolrInfo>>> solrfutureList = new ArrayList<>(maxId);
        for (int i = 1; i < index; i++) {
            int startId = i + 1000 * (i - 1);
            int endId = startId + 999;
            MongoUserTask mongoUserTask = new MongoUserTask(startId, endId);
            FutureTask<List<UserMongoInfo>> mongofuture = new FutureTask<>(mongoUserTask);
            executorService.submit(mongofuture);
            mongofutureList.add(mongofuture);

            MysqlUserTask mysqlUserTask = new MysqlUserTask(startId, endId);
            FutureTask<List<UserMysqlInfo>> mysqlfuture = new FutureTask<>(mysqlUserTask);
            executorService.submit(mysqlfuture);
            mysqlfutureList.add(mysqlfuture);

            SolrUserTask solrUserTask = new SolrUserTask(startId, endId);
            FutureTask<List<UserSolrInfo>> solrfuture = new FutureTask<>(solrUserTask);
            executorService.submit(solrfuture);
            solrfutureList.add(solrfuture);

        }
        executorService.shutdown();
        try {
            for (FutureTask<List<UserMongoInfo>> temp : mongofutureList) {
                List<UserMongoInfo> partUserMongoList = temp.get();
                userMongoList.addAll(partUserMongoList);
            }
            for (FutureTask<List<UserMysqlInfo>> temp : mysqlfutureList) {
                List<UserMysqlInfo> partUserMysqlList = temp.get();
                userMysqlList.addAll(partUserMysqlList);
            }
            for (FutureTask<List<UserSolrInfo>> temp : solrfutureList) {
                List<UserSolrInfo> partUserMongoList = temp.get();
                userSolrList.addAll(partUserMongoList);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        compare(userMongoList, userSolrList, userMysqlList);
    }

/*	private void compare(List<UserMongoInfo> userMongoList, List<UserSolrInfo> userSolrList,
			List<UserMysqlInfo> userMysqlList) {
		Map<Long, UserMongoInfo> mongoMap = userMongoList.stream()
				.collect(Collectors.toMap(UserMongoInfo::getId, userMongoInfo -> userMongoInfo));
		Map<Long, UserSolrInfo> solrMap = userSolrList.stream()
				.collect(Collectors.toMap(UserSolrInfo::getId, userSolrInfo -> userSolrInfo));
		for (UserMysqlInfo temp : userMysqlList) {
			UserMongoInfo userMongoInfo = mongoMap.get(temp.getId());
			UserSolrInfo userSolrInfo = solrMap.get(temp.getId());
			Map<String, Object> solrWrongMap = new HashMap<>(32);
			Map<String, Object> mongoWrongMap = new HashMap<>(32);
			AbstractBasicCompareService.compare(temp, userMongoInfo, mongoWrongMap);
			AbstractBasicCompareService.compare(temp, userSolrInfo, solrWrongMap);

			userMongoDataHandler.updateInfoByIdThroughHashMap(temp.getId(), mongoWrongMap);

			userSolrDataHandler.updateInfoByIdThroughHashMap(temp.getId(), solrWrongMap);

		}
	}
*/
}
